/*
 * The MIT License
 *
 * Copyright 2014 venkat.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.tfvc.workingfolder;

import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.swing.JComponent;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.modules.tfsnb.tfvc.workingfolder.treeview.WorkingFolderServerItemTreeViewPanel;
import org.openide.DialogDisplayer;
import org.openide.WizardDescriptor;

// An example action demonstrating how the wizard could be called from within
// your code. You can move the code below wherever you need, or register an action:
// @ActionID(category="...", id="org.netbeans.modules.tfsnb.tfvc.workingfolder.WorkingFolderWizardAction")
// @ActionRegistration(displayName="Open Editor Wizard")
// @ActionReference(path="Menu/Tools", position=...)
public final class WorkingFolderWizardAction implements ActionListener {
    
    public WorkingFolderWizardAction(final VersionControlClient vcc, final TFSTeamProjectCollection teamProjectCollection) {
        this.versionControlClient = vcc;
        this.teamProjectCollection = teamProjectCollection;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        List<WizardDescriptor.Panel<WizardDescriptor>> panels = new ArrayList<>();
        serverDirectoryWizardPanel = new WorkingFolderWizardServerDirectoryWizardPanel(versionControlClient, teamProjectCollection);
        panels.add(serverDirectoryWizardPanel);
        panels.add(new WorkingFolderWizardLocalDirectoryWizardPanel());
        String[] steps = new String[panels.size()];
        for (int i = 0; i < panels.size(); i++) {
            Component c = panels.get(i).getComponent();
            // Default step name to component name of panel.
            steps[i] = c.getName();
            if (c instanceof JComponent) { // assume Swing components
                JComponent jc = (JComponent) c;
                jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, i);
                jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps);
                jc.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
                jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DISPLAYED, true);
                jc.putClientProperty(WizardDescriptor.PROP_CONTENT_NUMBERED, true);
            }
        }
        WizardDescriptor wiz = new WizardDescriptor(new WorkingFolderWizardIterator(panels));
        // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
        wiz.setTitleFormat(new MessageFormat("{0}"));
        wiz.setTitle("New working folder mapping wizard");
        if (DialogDisplayer.getDefault().notify(wiz) == WizardDescriptor.FINISH_OPTION) {
            // do something
        }
    }
    
    private static class WorkingFolderWizardIterator extends WizardDescriptor.ArrayIterator<WizardDescriptor> 
        implements WizardDescriptor.ProgressInstantiatingIterator<WizardDescriptor>, PropertyChangeListener {

        private ProgressHandle progressHandle;
        
        private WorkingFolderWizardIterator(List<WizardDescriptor.Panel<WizardDescriptor>> panels) {
            super(panels);
        }

        @Override
        public Set instantiate(ProgressHandle ph) throws IOException {
            progressHandle = ph;
            serverDirectoryWizardPanel.getComponent().addPropertyChangeListener("showProgressBar", this);
            return Collections.emptySet();
        }

        @Override
        public Set instantiate() throws IOException {
            throw new AssertionError("instantiate(ProgressHandle) should have been called"); //NOI18N
        }

        @Override
        public void initialize(WizardDescriptor wd) {
            throw new AssertionError("instantiate(ProgressHandle) should have been called"); //NOI18N
        }

        @Override
        public void uninitialize(WizardDescriptor wd) {
            throw new AssertionError("instantiate(ProgressHandle) should have been called"); //NOI18N
        }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if((boolean) evt.getNewValue()) {
                progressHandle.start();
                progressHandle.progress(WorkingFolderServerItemTreeViewPanel.PROGRESS_TEXT);
            } else {
                progressHandle.finish();
            }
        }


    }
    
    private static WorkingFolderWizardServerDirectoryWizardPanel serverDirectoryWizardPanel;
    private final VersionControlClient versionControlClient;
    private final TFSTeamProjectCollection teamProjectCollection;
}
